<?php

/**
 * SigmaCMS - Content Management System
 *
 * Copyright (C) 2008 Pavol Biely
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    SigmaCMS
 * @author     Pavol Biely <pavolbiely@gmail.com>
 * @copyright  2008 Pavol Biely
 * @license    http://www.gnu.org/licenses/gpl.txt   GNU General Public License
 * @link       http://pabi3.com/
 * @since      0.1.0
 */

final class Admin_Logs
{
	/**
	 * Initialization
	 *
	 * @return void
	 */
	public function initialize()
	{
		Admin::header(_ADMIN_LOGS);

		echo "<h1>"._ADMIN_LOGS."</h1>\n";

		if (isset($_GET['filename']) && preg_match('/^([a-z0-9_-]+)\.log$/i',_get('filename'))) {
			// array with lines from log file
			$file = array();

			// path to directory with logs
			$path = dirname(__FILE__).'/../../'.cfg::DIR_LOGS;

			// absolute path to log
			$log = $path._get('filename');

			// does the log file exist?
			if (is_file($log)) {
				// number of lines to read
				$lines = 100;

				// open log file
				$handle = fopen($log, 'r');

				// starting position of seeking
				$pos = -1;

				// temporary value used to save log
				$tmp = NULL;

				// rewind the position of a file pointer
				$rewind = false;

				// load log file
				while ($lines > 0) {
					// take newline as a new log
					while ($tmp != "\n") {
						if (fseek($handle, $pos, SEEK_END) == -1) {
							$rewind = true;
							break;
						}
						$tmp = fgetc($handle);
						$pos--;
					}

					// rewind a file pointer if needed
					if ($rewind) rewind($handle);

					// save log to array
					$tmp = fgets($handle);

					// log cannot be empty line
					if (!empty($tmp)) {
						// create new log
						$file[] = $tmp;
					}

					// decrement lines
					$tmp = NULL;
					$lines--;

					// break loop if needed
					if($rewind) break;
				}

				// close file
				fclose($handle);

				// are there any logs?
				if (count($file) > 0) {
					echo "<table>\n";
					echo "<tr>\n";
					echo "<th>"._ADMIN_LOGS_DATE."</th>\n";
					echo "<th>"._ADMIN_LOGS_IPADDRESS."</th>\n";
					echo "<th>"._ADMIN_LOGS_MEMBER."</th>\n";
					echo "<th>"._ADMIN_LOGS_MESSAGE."</th>\n";
					echo "</tr>\n";

					// print logs
					for ($i = 0; $i < count($file); $i++) {
						// if the array item does not exist, terminate loop
						if (!isset($file[$i])) break;

						// split date, ip address and content by special string
						list($date, $ip, $member, $message) = explode('<%>',$file[$i]);

						// print log
						echo "<tr>\n";
						echo "<td class='center' width='20%'>$date</td>\n";
						echo "<td class='center' width='15%'>$ip</td>\n";
						echo "<td class='center' width='15%'>".(!empty($member) ? $member : "-")."</td>\n";
						echo "<td width='50%'>$message</td>\n";
						echo "</tr>\n";
					}
					echo "</table>\n";
				} else {
					echo "<p class='red'>"._ADMIN_LOGS_EMPTY."</p>\n";
				}
			} else {
				echo "<p class='red'>"._ADMIN_LOGS_NOT_FOUND."</p>\n";
			}
		} else {
			Admin::redirect('index.php');
		}

		Admin::footer();
	}

	/**
	 * Menu
	 *
	 * @return void
	 */
	public function menu()
	{
		echo "<h2>"._ADMIN_LOGS."</h2>\n";
		echo "<ul>\n";
		echo "<li><a href='index.php?action=logs&amp;page=truncate&amp;filename="._hsc(_get('filename'))."'>"._ADMIN_LOGS_TRUNCATE."</a></li>\n";
		echo "</ul>\n";
	}

	/**
	 * Truncate logs
	 *
	 * @return void
	 */
	public function page_truncate()
	{
		// path to directory with logs
		$path = dirname(__FILE__).'/../../'.cfg::DIR_LOGS;

		// absolute path to log
		$log = $path.Media::clearPath(_get('filename'));

		// truncate log if it exist
		if (file_exists($log) && preg_match('/^[a-z0-9_-]+\.log$/i',basename($log))) {
			@unlink($log);
		}

		Admin::redirect('index.php?action=logs');
	}
}
